from django.shortcuts import render
from django.views import View
from django.http import JsonResponse
from books.models import BookInfo
from rest_framework.views import View
from rest_framework.generics import GenericAPIView,ListAPIView,CreateAPIView,UpdateAPIView,DestroyAPIView,RetrieveAPIView

import json
# Create your views here.

class Books(View):
    """
        获取所有图书和保存图书
    """

    def get(self,requst):
       # 1、查询所有图书
        books=BookInfo.objects.all()

        # 2、返回图书数据
        book_list=[]
        for book in books:
            book_list.append({
                'id':book.id,
                'btitle':book.btitle,
                'bread':book.bread,
                'bpub_date':book.bpub_date,
            })
        return JsonResponse(book_list,safe=False)

    def post(self,request):
        # 1、获取前端数据
        data=request.body.decode()
        data_dict=json.loads(data)
        # 2、验证数据
        btitle=data_dict.get('btitle')
        bpub_date=data_dict.get('bpub_date')
        if btitle is None or bpub_date is None:
            return JsonResponse({'error':'缺少参数'},status=400)
        # 3、保存数据
        book=BookInfo.objects.create(btitle=btitle,bpub_date=bpub_date)
        # 4、返回结果

        return JsonResponse(
            {
                'id': book.id,
                'btitle': book.btitle,
                'bread': book.bread,
                'bpub_date': book.bpub_date,
            }
        )

class Book(View):
    """
        获取单一图书和修改删除
    """

    def get(self, requst,pk):
        #1、查询数据
        try:
            book=BookInfo.objects.get(id=pk)
        except:
            return JsonResponse({'error': '数据错误'}, status=400)
        # 返回数据
        return JsonResponse(
            {
                'id': book.id,
                'btitle': book.btitle,
                'bread': book.bread,
                'bpub_date': book.bpub_date,
            }
        )


    def put(self, request,pk):
        # 1、获取前端数据
        data = request.body.decode()
        data_dict = json.loads(data)
        # 2、验证数据
        btitle = data_dict.get('btitle')
        bpub_date = data_dict.get('bpub_date')
        if btitle is None or bpub_date is None:
            return JsonResponse({'error': '缺少参数'}, status=400)
        # 3、更新数据
        # 查询数据
        try:
            book = BookInfo.objects.get(id=pk)
        except:
            return JsonResponse({'error': '数据错误'}, status=400)

        book.btitle=btitle
        book.bpub_date=bpub_date
        book.save()


        # 4、返回结果
        return JsonResponse(
            {
                'id': book.id,
                'btitle': book.btitle,
                'bread': book.bread,
                'bpub_date': book.bpub_date,
            }
        )

    def delete(self, request,pk):
        # 1、查询数据
        try:
            book = BookInfo.objects.get(id=pk)
        except:
            return JsonResponse({'error': '数据错误'}, status=400)

        book.is_delete=True
        book.save()
        return JsonResponse({})